package com.zhdl.modules.demo.DCHTProcess;

import com.project.common.util.DelayUtil;

import com.zhdl.modules.process.common.ProcessTimeout;
import com.zhdl.modules.process.dto.ProcessBean;
import com.zhdl.modules.process.service.ProjectConfigProcessService;
import com.zhdl.modules.process.task.CustomizeTask;
import com.zhdl.modules.process.task.DynamicTask;
import com.zhdl.modules.process.verify.DistanceVerify;
import com.zhdl.modules.process.verify.PlCBitValueVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ProcessInitService {
    @Resource
    ProjectConfigProcessService configurationPLcTest;

    @Resource
    DynamicTask dynamicTask;
    @Resource
    CameraInterface cameraInterface;

    public List<ProcessBean> initTestProcessList() {
        List<ProcessBean> list = new ArrayList<>();

        list.add(new ProcessBean("初始化", 0)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV1_1#料仓气缸", true).doSomeThing();
                    new CylinderControlDo("SV2_2#料仓气缸", true).doSomeThing();
                    new CylinderControlDo("SV3_触指横移气缸",false).doSomeThing();
                    new CylinderControlDo("SV4_触指顶升气缸",false).doSomeThing();
                    new CylinderControlDo("SV5_触指撑开气缸",true).doSomeThing();
                    new CylinderControlDo("SV6_套环提升气缸",true).doSomeThing();
                    new CylinderControlDo("SV7_套环横移气缸",false).doSomeThing();
                    new CylinderControlDo("SV8_套触指气缸",true).doSomeThing();

                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "1#料仓夹紧气缸开到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "2#料仓夹紧气缸开到位"))
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指横移气缸关到位"))
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"触指顶升气缸关到位"))
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"撑开气缸开到位"))
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"套环提升气缸开到位"))
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"套环横移气缸开到位"))
                        .addProcessVerify(new PlCBitValueVerify(configurationPLcTest,"套触指气缸开到位"))
                );





        list.add(new ProcessBean("SV4_触指顶升气缸打开", 1)
                .addDoSomeThing(new CylinderControlDo("SV4_触指顶升气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指顶升气缸开到位")));


        list.add(new ProcessBean("默认机器人已经取料固定至套环装配位,SV1_1#料仓 SV2_2#料仓气缸控关闭", 2)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV1_1#料仓气缸", false).doSomeThing();
                    new CylinderControlDo("SV2_2#料仓气缸", false).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "1#料仓夹紧气缸关到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "2#料仓夹紧气缸关到位")));
        list.add(new ProcessBean("默认机器人已经取料固定至套环装配位,SV1_1#料仓 SV2_2#料仓气缸控制打开", 3)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV1_1#料仓气缸", true).doSomeThing();
                    new CylinderControlDo("SV2_2#料仓气缸", true).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "1#料仓夹紧气缸开到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "2#料仓夹紧气缸开到位")));
        list.add(new ProcessBean("SV4_触指顶升气缸关闭", 4)
                .addDoSomeThing(new CylinderControlDo("SV4_触指顶升气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指顶升气缸关到位")));
        list.add(new ProcessBean("SV3_触指横移气缸打开", 5)
                .addDoSomeThing(new CylinderControlDo("SV3_触指横移气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指横移气缸开到位")));
//        list.add(new ProcessBean("SV7_套环横移关闭", 6)
//                .addDoSomeThing(new CylinderControlDo("SV7_套环横移气缸", false))
//                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环横移气缸关到位")));
        list.add(new ProcessBean("SV6_套环提升打开运动至取触指位置", 6)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸关到位")));
        list.add(new ProcessBean("SV5_触指撑开打开", 7)
                .addDoSomeThing(new CylinderControlDo("SV5_触指撑开气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "撑开气缸关到位")));
        list.add(new ProcessBean("SV6_套环提升关闭-提升", 8)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸开到位")));
        list.add(new ProcessBean("SV7_套环横移打开运动至套环位置", 9)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV7_套环横移气缸", true).doSomeThing();
                    new CylinderControlDo("SV3_触指横移气缸", false).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环横移气缸开到位"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "触指横移气缸关到位")));
        list.add(new ProcessBean("SV6_套环提升打开运动至取触指位置", 10)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸关到位")));
        list.add(new ProcessBean("SV8_套触指气缸关闭夹紧", 11)
                .addDoSomeThing(new CylinderControlDo("SV8_套触指气缸", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套触指气缸关到位")));
        list.add(new ProcessBean("SV5_触指撑开关闭", 12)
                .addDoSomeThing(new CylinderControlDo("SV5_触指撑开气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "撑开气缸开到位")));
        list.add(new ProcessBean("SV6_套环提升关闭-提升", 13)
                .addDoSomeThing(new CylinderControlDo("SV6_套环提升气缸", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环提升气缸开到位")));
        list.add(new ProcessBean("SV7_套环横移关闭", 14)
                .addDoSomeThing(() -> {
                    new CylinderControlDo("SV7_套环横移气缸", false).doSomeThing();
                    new CylinderControlDo("SV8_套触指气缸", true).doSomeThing();
                })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套环横移气缸关到位"))
                        .addProcessTimeout(2000, new ProcessTimeout() {
                            @Override
                            public void timeout(String reason, ProcessBean processBean) {

                            }
                        })
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "套触指气缸开到位")));






        list.add(new ProcessBean("SV9_旋转夹紧位夹爪加紧", 15)
                .addDoSomeThing(new CylinderControlDo("SV9_旋转夹紧位夹爪", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位夹爪气缸关到位")));
        list.add(new ProcessBean("旋转加紧横向电机 顶级触头工件", 16)
                .addDoSomeThing(new MotorABSRunDo("旋转夹紧位横移轴", 1))
                .addProcessVerify(new DistanceVerify(Float.parseFloat(configurationPLcTest.getValue("旋转夹紧位横移轴_MoveAbs_Pos[1]").getValue()), 0.1f, configurationPLcTest, "旋转夹紧位横移轴_CurrentPos"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位横移轴_MoveAbs_Done[1]")));
        list.add(new ProcessBean("打螺丝位横移轴 移动至相机拍照位置", 17)
                .addDoSomeThing(new MotorABSRunDo("打螺丝位横移轴", 1))
                .addProcessVerify(new DistanceVerify(Float.parseFloat(configurationPLcTest.getValue("打螺丝位横移轴_MoveAbs_Pos[1]").getValue()), 0.1f, configurationPLcTest, "打螺丝位横移轴_CurrentPos"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺丝位横移轴_MoveAbs_Done[1]")));
        list.add(new ProcessBean("打开相机拍照", 18)
                .addDoSomeThing(() -> {
                    log.info("模拟相机拍照");
                    cameraInterface.upDataValue(CameraInterface.CAMERA_RESULT1, "0");
                    DelayUtil.delayedExecution(2000, () -> cameraInterface.upDataValue(CameraInterface.CAMERA_RESULT1, "1"));
                })
                .addProcessVerify(new PlCBitValueVerify(cameraInterface, CameraInterface.CAMERA_RESULT1)));
        list.add(new ProcessBean("打螺丝位横移轴 移动至打螺丝位置", 19)
                .addDoSomeThing(new MotorABSRunDo("打螺丝位横移轴", 2))
                .addProcessVerify(new DistanceVerify(Float.parseFloat(configurationPLcTest.getValue("打螺丝位横移轴_MoveAbs_Pos[2]").getValue()), 0.1f, configurationPLcTest, "打螺丝位横移轴_CurrentPos"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺丝位横移轴_MoveAbs_Done[2]")));
        list.add(new ProcessBean("SV10_打螺栓位Z轴下降至工作位", 20)
                .addDoSomeThing(new CylinderControlDo("SV10_打螺栓位Z轴", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺栓位Z轴气缸开到位")));
        list.add(new ProcessBean("模拟打螺丝过程.....", 21)
                .addDoSomeThing(() -> {
                    cameraInterface.upDataValue(CameraInterface.DO_LUOSI_COMPLETE, "0");
                    DelayUtil.delayedExecution(3000, () -> cameraInterface.upDataValue(CameraInterface.DO_LUOSI_COMPLETE, "1"));
                })
                .addProcessVerify(new PlCBitValueVerify(cameraInterface, CameraInterface.DO_LUOSI_COMPLETE))
        );
        list.add(new ProcessBean("SV10_打螺栓位Z轴上升至等待位", 22)
                .addDoSomeThing(new CylinderControlDo("SV10_打螺栓位Z轴", false))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺栓位Z轴气缸关到位")));
        list.add(new ProcessBean("打螺丝位横移轴 移动至等待位", 23)
                .addDoSomeThing(new MotorABSRunDo("打螺丝位横移轴", 0))
                .addProcessVerify(new DistanceVerify(Float.parseFloat(configurationPLcTest.getValue("打螺丝位横移轴_MoveAbs_Pos[0]").getValue()), 0.1f, configurationPLcTest, "打螺丝位横移轴_CurrentPos"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "打螺丝位横移轴_MoveAbs_Done[0]")));
        list.add(new ProcessBean("SV9_旋转夹紧位夹爪松开", 24)
                .addDoSomeThing(new CylinderControlDo("SV9_旋转夹紧位夹爪", true))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位夹爪气缸开到位")));
        list.add(new ProcessBean("旋转加紧横向电机 回到等待位", 25)
                .addDoSomeThing(new MotorABSRunDo("旋转夹紧位横移轴", 0))
                .addProcessVerify(new DistanceVerify(Float.parseFloat(configurationPLcTest.getValue("旋转夹紧位横移轴_MoveAbs_Pos[1]").getValue()), 0.1f, configurationPLcTest, "旋转夹紧位横移轴_CurrentPos"))
                .addProcessVerify(new PlCBitValueVerify(configurationPLcTest, "旋转夹紧位横移轴_MoveAbs_Done[0]")));

        return list;
    }

    public void startProcess(int step) {
        List<ProcessBean> processBeans = initTestProcessList();
        ProcessBean processBean = processBeans.get(step);
        processBean.getProcessDo().doSomeThing();
        dynamicTask.startTask(new CustomizeTask(processBean), 100);
    }
}
